package indi.wrenn.studentsystem.frame;

import indi.wrenn.studentsystem.bean.User;
import indi.wrenn.studentsystem.dao.ManageHelper;
import indi.wrenn.studentsystem.util.WindowUtil;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * 修改密码对话框
 * 提供用户修改密码功能
 */
public class UpdatePasswordFrame extends JDialog{

	// 界面组件定义
	private JLabel password_Label;        // "原密码"标签
	private JLabel newpassword_Label;     // "新密码"标签
	private JLabel repassword_Label;      // "确认密码"标签
	private JPasswordField password_Text; // "原密码"密码框
	private JPasswordField newpassword_Text; // "新密码"密码框
	private JPasswordField repassword_Text; // "确认密码"密码框
	private JButton confirm_Button;       // "确定"按钮
	private JButton cancel_Button;        // "取消"按钮
	private JDialog jd;                   // 当前窗口引用
	private User user;                    // 当前用户对象

	/**
	 * 构造函数
	 * @param owner 父窗口
	 * @param title 对话框标题
	 * @param modal 是否为模态对话框
	 * @param user 当前用户对象
	 */
	public UpdatePasswordFrame(JFrame owner, String title, boolean modal, User user){
		super(owner, title, modal);
		this.user = user;
		this.jd = this;
		this.setLayout(null);        // 设置为绝对布局
		this.setSize(400, 300);      // 设置窗口大小

		// 获取内容面板并设置背景色
		Container c = this.getContentPane();
		c.setBackground(Color.WHITE);

		// 初始化原密码标签和密码框
		password_Label = new JLabel("原密码:");
		password_Label.setBounds(100, 60, 50, 20);
		c.add(password_Label);

		password_Text = new JPasswordField();
		password_Text.setBounds(160, 60, 120, 20);
		password_Text.grabFocus();  // 获取焦点
		c.add(password_Text);

		// 初始化新密码标签和密码框
		newpassword_Label = new JLabel("新密码:");
		newpassword_Label.setBounds(100, 110, 50, 20);
		c.add(newpassword_Label);

		newpassword_Text = new JPasswordField();
		newpassword_Text.setBounds(160, 110, 120, 20);
		c.add(newpassword_Text);

		// 初始化确认密码标签和密码框
		repassword_Label = new JLabel("确认密码");
		repassword_Label.setBounds(100, 162, 70, 20);
		c.add(repassword_Label);

		repassword_Text = new JPasswordField();
		repassword_Text.setBounds(160, 162, 120, 20);
		c.add(repassword_Text);

		// 初始化确定按钮
		confirm_Button = new JButton("确定");
		confirm_Button.setBounds(90, 210, 60, 20);

		// 为确定按钮添加事件监听器
		confirm_Button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 获取用户输入的密码信息
				String password = new String(password_Text.getPassword());
				String repassword = new String(repassword_Text.getPassword());
				String newpassword = new String(newpassword_Text.getPassword());

				// 数据验证
				if(password.equals("")){
					JOptionPane.showMessageDialog(jd, "原密码不能为空", "", JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(newpassword.equals("")){
					JOptionPane.showMessageDialog(jd, "新密码不能为空", "", JOptionPane.WARNING_MESSAGE);
					return;
				}
				if(repassword.equals("")){
					JOptionPane.showMessageDialog(jd, "确认密码不能为空", "", JOptionPane.WARNING_MESSAGE);
					return;
				}

				// 检查新密码和确认密码是否一致
				if(repassword.equals(newpassword)){
					// 验证原密码是否正确
					ManageHelper helper = new ManageHelper();
					user.setPassword(password);

					if(helper.Login(user)){
						// 原密码正确，执行密码修改
						helper.update_Password(user, newpassword);
						JOptionPane.showMessageDialog(jd, "修改密码成功");
						jd.dispose();  // 关闭当前对话框
						return;
					} else {
						// 原密码不正确
						JOptionPane.showMessageDialog(jd, "原密码不正确", "", JOptionPane.WARNING_MESSAGE);
						Reset();  // 重置输入框
						return;
					}
				} else {
					// 新密码和确认密码不一致
					JOptionPane.showMessageDialog(jd, "两次输入密码不一致", "", JOptionPane.WARNING_MESSAGE);
					Reset();  // 重置输入框
					return;
				}
			}
		});
		c.add(confirm_Button);

		// 初始化取消按钮
		cancel_Button = new JButton("取消");
		cancel_Button.setBounds(250, 210, 60, 20);

		// 为取消按钮添加事件监听器
		cancel_Button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				jd.dispose();  // 关闭当前对话框
			}
		});
		c.add(cancel_Button);

		// 设置窗口属性
		this.setResizable(false);  // 设置窗口不可调整大小
		WindowUtil.setFrameCenter(this);  // 设置窗口居中显示
		this.setVisible(true);     // 设置窗口可见
	}

	/**
	 * 重置方法 - 清空所有密码输入框
	 */
	public void Reset(){
		password_Text.setText("");
		repassword_Text.setText("");
		newpassword_Text.setText("");
	}
}